#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>

using namespace std;

#define SERVER_PORT 8888
#define BUFF_LINE 512
#define SERVER_IP "10.0.16.2"

void
udp_msg_sender(int fd, struct sockaddr* dst)
{
	socklen_t len;
	struct sockaddr_in src;
	while(1)
	{
		char buf[BUFF_LINE] = "TEST UDP MSG!\n";
		len = sizeof(*dst);
		printf("client:%s\n", buf);
		sendto(fd, buf, BUFF_LINE, 0, dst, len);
		memset(buf, 0, BUFF_LINE);
		recvfrom(fd, buf, BUFF_LINE, 0, (struct sockaddr*)&src, &len);
		printf("server:%s\n", buf);
		sleep(1);
	}
}

/*
	client:
		socket ->  sendto -> recvfrom -> close
*/

int main(int argc, char **argv)
{
	int client_fd;
	struct sockaddr_in ser_addr;

	client_fd = socket(AF_INET, SOCK_DGRAM, 0);
	if(client_fd<0)
	{
		printf("create socket fail!");
		return -1;	
	}
	
	memset(&ser_addr, 0, sizeof(ser_addr));
	ser_addr.sin_family = AF_INET;
	//ser_addr.sin_addr.s_addr = htonl(SERVER_IP);
	ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	ser_addr.sin_port = htons(SERVER_PORT);

	udp_msg_sender(client_fd, (struct sockaddr*)&ser_addr);

	close(client_fd);

	return 0;
}












